/*
Copyright 2007 Julian Asamer

This file is part of Fractalicious.

    Fractalicious is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Fractalicious is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Fractalicious.  If not, see <http://www.gnu.org/licenses/>.
*/

#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import "FAlgorithm.h"
#import "DrawingData.h"
#import "FractalController.h"
#import "Vector.h"
#import "FRecalcTypes.h"
#import "FFastAlgorithm.h"


@implementation FAlgorithm
	-(id) init {
		[super init];
		[self setStartingLinesSelector: @selector(getStartLines)];
		[self setTransformatorsSelector: @selector(getTransformers)];
		//data = [DrawingData sharedInstance];
		data = nil;
		return self;
	}
	
	//Depreceated
	// -(void) recWithLine:(Line2D *) line level: (int) lv {
	// 	if (rec_count < arr_count) {
	// 		sLine = [data->lines objectAtIndex: rec_count];
	// 		sLine->p->x = line->p->x;
	// 		sLine->p->y = line->p->y;
	// 		sLine->q->x = line->q->x;
	// 		sLine->q->y = line->q->y;	
	// 	} else {
	// 		[data->lines addObject: line];
	// 	}
	// 	rec_count++;
	// 	if (([line length]>_exactness)&&(rec_count<4000)) {
	// 		int i;
	// 		for (i=0; i < [transformers count]; i++) {
	// 			[self recWithLine: [[transformers objectAtIndex: i] transformLine: line] 
	// 					level: ++lv];
	// 		}
	// 	}
	// }
	
	-(void) calculateFractal:(RecalcType) type {
		NSDate * date = [NSDate date];
		
		if (type==0) 
			_exactness = 1.0f;
		if (type==Fast)
			_exactness = 5.0f;
		if (type==Drawonly) 
			return;
		
		//arr_count = [data->lines count];
		
		//rec_count = 0;	
		NSMutableArray *t = [dataSource performSelector: _getTransformers];
		NSMutableArray *s = [dataSource performSelector: _getStartLines];
		
		//NSLog(@"Starting FFastCalc");
		//if (data!=nil) [data release];
		DrawableFractal *o_data = data;
		
		data = calculate(Full, s, t);
		
		
		if (data==nil) {
			NSLog(@"data is nil right after calculation.");
		}
		// int i;
		// 	for (i=0; i<[s count]; i++) {
		// 		[self recWithLine: [s objectAtIndex: i] level: 0];
		// 		//recWithLine([s objectAtIndex: i], 0);
		// 	}
		// 
		// 	data->actualLength = rec_count-1;
		// 	//NSLog(@"Counts: rec: %i; arr: %i", rec_count, arr_count)
		// 	/*for (i=rec_count-1; i<arr_count; i++) {
		// 		[data->lines removeObjectAtIndex: rec_count-1];
		// 	}*/
		// 
		// 	
		// 
		// 
		// if ([delegate respondsToSelector: @selector(calculationFinished)] )
		//     [delegate performSelector: @selector(calculationFinished)];
		// else 
		// 	NSLog(@"delegate does not respond");
			
		if ([delegate respondsToSelector: @selector(calculationFinished:)] )
		    [delegate performSelector: @selector(calculationFinished:) withObject: data];
		else 
			NSLog(@"delegate does not respond");
		if (o_data!=nil) [o_data release];
	}
	
	
	-(void) setStartingLinesSelector: (SEL)selector {
		_getStartLines = selector;
	}
	-(void) setTransformatorsSelector: (SEL)selector {
		_getTransformers = selector;
	}
	-(void) setDataSource: (id) object {
		dataSource = object;
	}
	-(void) setDelegate: (id) d {
		delegate = d;
	}
	-(id) delegate {
		return delegate;
	}

	
@end;